package com.fresh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fresh.entity.FreshCategory;
import com.fresh.mapper.CategoryMapper;
import com.fresh.service.CategoryService;
import com.fresh.util.RecursionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, FreshCategory> implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RecursionUtil recursionUtil;

    @Override
    public List<FreshCategory> getAll() {
        List<FreshCategory> categoryList = categoryMapper.selectAll();
        return categoryList;
    }

    //TODO 事务
    @Override
    public void updateStatusById(FreshCategory freshCategory) {
        FreshCategory category = categoryMapper.selectOne(freshCategory.getId());
        List<Integer> ids = recursionUtil.getIds(category.getCategories());
        ids.add(freshCategory.getId());
//        System.out.println(ids);
        UpdateWrapper<FreshCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status",freshCategory.getStatus());
        updateWrapper.in("id",ids);
        categoryMapper.update(null,updateWrapper);
    }

    @Override
    public List<FreshCategory> getMainCategory() {
        QueryWrapper<FreshCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("pid");
        List<FreshCategory> categoryList = categoryMapper.selectList(queryWrapper);
        return categoryList;
    }

    @Override
    public List<FreshCategory> getDeputyCategory() {
        QueryWrapper<FreshCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("pid");
        List<FreshCategory> categoryList = categoryMapper.selectList(queryWrapper);
        return categoryList;
    }


}
